<?php

// Add our function to the widgets_init hook.
add_action('widgets_init', 'fazio_widgets');

// Function that registers all Fazio widgets.
function fazio_widgets() {
	register_widget('topCommented_Widget');
	register_widget('searchFazio_Widget');
	register_widget('subscribe_Widget');
}

// TopCommentedPosts widget
class topCommented_Widget extends WP_Widget
{

	function topCommented_Widget()
	{
		parent::WP_Widget(false, $name = __('Top Commented Posts', 'fazio'));
	}

	/** @see WP_Widget::widget */
	function widget($args, $instance)
	{
		extract( $args );
		
		// Extract config vars
		$title = apply_filters('widget_title', $instance['title']);
		$quantity_to_show = $instance['quantity_to_show'];
		if(!is_numeric($quantity_to_show) OR $quantity_to_show == '0')
		{
			$quantity_to_show = 5;
		}

		// Make the widget
		echo $before_widget;
		
		echo $before_title;
		
		if($title) {
			echo $title;
		} else {
			_e('Top Commented Posts', 'fazio');
		}
			
		echo $after_title;
		
		$top = new WP_Query();
		$top->query('showposts=' . $quantity_to_show . '&orderby=comment_count');
		
		if($top->have_posts())
		{
		?>
			<ul>
				<?php
				while ($top->have_posts())
				{
					$top->the_post();
				?>
					<li>
						<a href="<?php the_permalink();?>"><?php the_title(); ?></a> (<?php echo get_comments_number(); ?>)
					</li>
		<?php
				}
		}
		?>
			</ul>

		<?php
		echo $after_widget;
	}

	/** @see WP_Widget::update */
	function update($new_instance, $old_instance)
	{				
		return $new_instance;
	}

	/** @see WP_Widget::form */
	function form($instance)
	{
		$instance = wp_parse_args((array)$instance, array('title' => '', 'quantity_to_show' => ''));

		$title = esc_attr($instance['title']);
		$quantity_to_show = esc_attr($instance['quantity_to_show']);
		?>
			<p>
				<label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?>
					<input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" />
				</label>
			</p>
			
			<p>
				<label for="<?php echo $this->get_field_id('quantity_to_show'); ?>"><?php _e('Quantity to show:'); ?>
					<input class="widefat" id="<?php echo $this->get_field_id('quantity_to_show'); ?>" name="<?php echo $this->get_field_name('quantity_to_show'); ?>" type="text" value="<?php echo $quantity_to_show; ?>" />
				</label>
			</p>
		<?php 
	}

}

// Search (Fazio styled) widget
class searchFazio_Widget extends WP_Widget {
	
	function searchFazio_Widget()
	{
		parent::WP_Widget(false, $name = __('Search (Fazio Styled)', 'fazio'));
	}

	/** @see WP_Widget::widget */
	function widget($args, $instance)
	{
		extract( $args );
		if(!empty($instance['search_field']))
		{
			$search_field = $instance['search_field'];
		} else {
			$search_field = __('Search here', 'fazio');
		}
		
		if(get_search_query() != ''){
			$search_query = get_search_query();
		}
		
		echo '<div id="search">';
		?>
		
		<form action="<?php bloginfo('url'); ?>" method="get">
			<input id="search_field" type="text" name="s"
				<?php if(!isset($search_query)) : ?>
					data-defaultvalue="<?php echo $search_field; ?>"
					value="<?php echo $search_field; ?>"
				<?php else : ?>
					value="<?php echo $search_query; ?>"
				<?php endif; ?>
				 />
			<input type="image" src="<?php bloginfo('template_directory'); ?>/images/search_button.png" name="search" />
		</form>

		<?php
		
		echo '</div>';
	}

	/** @see WP_Widget::update */
	function update($new_instance, $old_instance)
	{			 
		return $new_instance;
	}

	/** @see WP_Widget::form */
	function form($instance)
	{
		$instance = wp_parse_args((array)$instance, array('search_field' => ''));
		
		$search_field = $instance['search_field'];
		?>
			<p><label for="<?php echo $this->get_field_id('search_field'); ?>"><?php _e('Default input value:'); ?> <input class="widefat" id="<?php echo $this->get_field_id('search_field'); ?>" name="<?php echo $this->get_field_name('search_field'); ?>" type="text" value="<?php echo $search_field; ?>" /></label></p>
		<?php 
	}
	
}


// Subscribe widget
class subscribe_Widget extends WP_Widget
{

	function subscribe_Widget()
	{
		parent::WP_Widget(false, $name = __('Subscribe', 'fazio'));
	}

	/** @see WP_Widget::widget */
	function widget($args, $instance)
	{
		extract( $args );
		
		// Extract config vars
		$title = apply_filters('widget_title', $instance['title']);
		$feed = $instance['feed'];
		$twitter = $instance['twitter'];
		$email = $instance['email'];
		$apply_box_style = $instance['apply_box_style'];

		// Open widget
		echo '<div id="social"';
		
		if($apply_box_style == 'on') :
			echo ' class="box"';
		endif;
		
		echo '>';

		// Show the title
		if($title && $apply_box_style == 'on')
		{
			echo $before_title, $title, $after_title;
		}

		// Show rss link
		if($feed)
		{
		?>
		
			<div class="social_item">
				<a href="http://feeds.feedburner.com/<?php echo $feed; ?>">
					<img src="<?php bloginfo('template_directory'); ?>/images/social/rss.png" alt="<?php _e('Subscribe using our rss', 'fazio'); ?>">
					<span><?php _e('Subscribe using our rss', 'fazio'); ?></span>
				</a>
			</div>
		
		<?php
		}

		// Show rss link
		if($twitter)
		{
		?>
		
			<div class="social_item">
				<a href="http://twitter.com/<?php echo $twitter; ?>">
					<img src="<?php bloginfo('template_directory'); ?>/images/social/twitter.png" alt="<?php _e('Follow us on twitter', 'fazio'); ?>">
					<span><?php _e('Follow us on twitter', 'fazio'); ?></span>
				</a>
			</div>
		
		<?php
		}
		
		// Show rss link
		if($email == 'on' && $feed)
		{
		?>
		
			<div class="social_item">
				<a href="http://feedburner.google.com/fb/a/mailverify?uri=<?php echo $feed; ?>" id="subscribe_by_email">
					<img src="<?php bloginfo('template_directory'); ?>/images/social/email.png" alt="<?php _e('Subscribe by email', 'fazio'); ?>">
					<span><?php _e('Subscribe by email', 'fazio'); ?></span>
				</a>
			</div>
		
		<?php
		}

		// Close widget
		echo '</div>';
	}

	/** @see WP_Widget::update */
	function update($new_instance, $old_instance)
	{				
		return $new_instance;
	}

	/** @see WP_Widget::form */
	function form($instance)
	{
		$instance = wp_parse_args((array)$instance, array('title' => '', 'feed' => '', 'twitter' => '', 'email' => '', 'apply_box_style' => 'on'));

		$title = esc_attr($instance['title']);
		$feed = esc_attr($instance['feed']);
		$twitter = esc_attr($instance['twitter']);
		$email = esc_attr($instance['email']);
		$apply_box_style = esc_attr($instance['apply_box_style']);
		?>
			<p>
				<label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:', 'fazio'); ?>
					<input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" />
				</label>
			</p>
			
			<p>
				<label for="<?php echo $this->get_field_id('feed'); ?>"><?php _e('Feedburner username:', 'fazio'); ?>
					<input class="widefat" id="<?php echo $this->get_field_id('feed'); ?>" name="<?php echo $this->get_field_name('feed'); ?>" type="text" value="<?php echo $feed; ?>" />
				</label>
			</p>
			
			<p>
				<label for="<?php echo $this->get_field_id('twitter'); ?>"><?php _e('Twitter username:', 'fazio'); ?>
					<input class="widefat" id="<?php echo $this->get_field_id('twitter'); ?>" name="<?php echo $this->get_field_name('twitter'); ?>" type="text" value="<?php echo $twitter; ?>" />
				</label>
			</p>
			
			<p>
				<label for="<?php echo $this->get_field_id('email'); ?>"><?php _e('Show "subscribe via email" link?: (Requires Feedburner username)', 'fazio'); ?>
					<input id="<?php echo $this->get_field_id('email'); ?>" name="<?php echo $this->get_field_name('email'); ?>" type="checkbox" <?php if($email == 'on') : ?>checked="checked"<?php endif; ?> />
				</label>
			</p>
			
			<p>
				<label for="<?php echo $this->get_field_id('apply_box_style'); ?>"><?php _e('Apply "normal" box style?:', 'fazio'); ?>
					<input id="<?php echo $this->get_field_id('apply_box_style'); ?>" name="<?php echo $this->get_field_name('apply_box_style'); ?>" type="checkbox" <?php if($apply_box_style == 'on') : ?>checked="checked"<?php endif; ?> />
				</label>
			</p>
		<?php 
	}

}